package com.cy.jxc.web;

import com.cy.jxc.vo.JsonResult;
import io.lettuce.core.RedisException;
import org.apache.shiro.ShiroException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.LockedAccountException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authz.AuthorizationException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import springfox.documentation.spring.web.json.Json;

@RestControllerAdvice
public class GlobalExecptionHandle {
    @ExceptionHandler(ShiroException.class)
    public JsonResult doHandleShiroExecption(ShiroException e){
        JsonResult jsonResult=new JsonResult();
        jsonResult.setStatus(0);
        if (e instanceof UnknownAccountException){
            jsonResult.setMessage("没有对应的用户名");
        }else if(e instanceof LockedAccountException) {
            jsonResult.setMessage("账户已被禁用");
        }else if(e instanceof IncorrectCredentialsException) {
            jsonResult.setMessage("密码不正确");
        }else if(e instanceof AuthorizationException) {
            jsonResult.setMessage("没有此操作权限");
        }else {
            jsonResult.setMessage("系统维护中");
        }
        e.printStackTrace();
        return jsonResult;
    }
    @ExceptionHandler(RuntimeException.class)
    public JsonResult getHandle(RuntimeException e){
        e.printStackTrace();
        return new JsonResult(e);
    }

    @ExceptionHandler(RedisException.class)
    public  JsonResult getRedsHandle(RedisException e){

        JsonResult jsonResult=new JsonResult();
        jsonResult.setStatus(0);
        jsonResult.setMessage("服务端异常");
        e.printStackTrace();
        return new JsonResult(e);
    }
}
